package com.cskaoyan.com.collection.map;

import java.util.HashMap;
import java.util.Objects;

public class DemoMap5 {
    public static void main(String[] args) {

        User zs1 = new User("zs", 18);
        User zs2 = new User("zs", 18);

        System.out.println(zs1.hashCode());
        System.out.println(zs2.hashCode());

        HashMap<User, Integer> map = new HashMap<>();

        map.put(zs1, 1);
        map.put(zs2, 2);

        System.out.println(map.size());

        // zs1.hashCode (460141958)  --> hash  (不一样) --> 下标
        // zs2.hashCode (1163157884) --> hash  (不一样) --> 下标

    }
}
class User{
    String name;
    int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
